package admin.controllers;

import helpers.StringHelper;

import java.io.IOException;
import java.net.URLDecoder;
import java.util.TreeMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import debugs.StaffDebug;

import models.StaffModel;

import admin.data.StaffData;
import admin.forms.StaffForm;

public class ProfileServlet extends HttpServlet{
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		HttpSession session = request.getSession();
		
		TreeMap check = new TreeMap();
		StaffData staffData = new StaffData();
		StaffModel user = new StaffModel();
		
		if (session.getAttribute("staff_logged_in") != null) {
			user = (StaffModel) session.getAttribute("staff_logged_in");
			try {
				user = (StaffModel) user.clone();
			} catch (CloneNotSupportedException e1) {
				//e1.printStackTrace();
			}
		}
		
		user.setPassword("");
		user.setOldpassword("");
		user.setRepassword("");
		
		request.setAttribute("form", "profile");
		request.setAttribute("check", check);
		request.setAttribute("user", user);
		try {
			request.getRequestDispatcher("/admin/pages/form_profile.jsp").forward(request, response);
		} catch (ServletException e) {
			e.printStackTrace();
		}
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		HttpSession session = request.getSession();
		
		String path = ((HttpServletRequest) request).getRequestURI();
		String action = "";
		try {
			action = URLDecoder.decode(path.replaceAll("/admin/profile/", ""),"UTF-8");
		} catch (Exception e) {
			response.sendRedirect("/admin/profile");
		}
		
		StaffData staffData = new StaffData();
		StaffModel user = new StaffModel();
		
		if (session.getAttribute("staff_logged_in") != null) {
			user = (StaffModel) session.getAttribute("staff_logged_in");
			try {
				user = (StaffModel) user.clone();
			} catch (CloneNotSupportedException e1) {
				//e1.printStackTrace();
			}
		}
		
		StaffForm staffForm = new StaffForm();
		TreeMap forms = new TreeMap();
		if (action.equalsIgnoreCase("password")) {
			forms = staffForm.checkValidPassword(request, user);
		} else {
			forms = staffForm.checkValidProfile(request, user);
		}
		
		user = (StaffModel) forms.get("user");
		TreeMap check = (TreeMap) forms.get("checkValid");
		
		if (check.size() == 0) {
			if (action.equalsIgnoreCase("password"))
				user.setPassword(StringHelper.md5(user.getPassword()));
			
			staffData.insert(user);
			session.setAttribute("staff_logged_in", user);
			if (action.equalsIgnoreCase("password")) {
				user.setPassword("");
				user.setOldpassword("");
				user.setRepassword("");
				request.setAttribute("success", "Bạn đã thay đổi mật khẩu thành công");
			} else {
				request.setAttribute("success", "Bạn đã thay đổi thông tin thành công");
			}
		}
		
		user.setPassword("");
		request.setAttribute("form", "profile");
		request.setAttribute("check", check);
		request.setAttribute("user", user);
		try {
			request.getRequestDispatcher("/admin/pages/form_profile.jsp").forward(request, response);
		} catch (ServletException e) {
			e.printStackTrace();
		}
	}
}
